﻿using System;
using System.Collections.Generic;
using System.Text;
using Core.Domain;
using Core.Domain.Lexems;

namespace Core.SyntaxAnalysis.Classes
{
    public class GOTO : Stmt
    {
        private readonly Lexem m_label;

        public Label Lbl;

        public GOTO(Lexem label)
        {
            m_label = label;
        }

        public override bool StmtSemanticCheck(ref List<CompilerError> errors)
        {
            Lbl = Env.FindLabel(m_label);
            bool res = Lbl != null; // если метка объявлена - то она не равна null
            if (!res)
            {
                errors.Add(new CompilerError(errors.Count + 1, String.Format(Resources.LabelWasNotDefinedErrorMessage, m_label.StrValue), m_label));
            }
            return res;
        }

        public override void StmtGenerate(StringBuilder s)
        {
            CodeGenerator.AppendGoto(s, Lbl);
        }
    }
}